草庐IT

c++ - 在库 API 中传递 std::string

全部标签

go - 如何在 docker registry api 中为带有标签的图像名称创建 map[string][]string?

我正在使用DockerRegistryAPI,首先使用Listrepositories列出注册表中的所有图像方法,然后在图像中循环以创建一个map[string][]string:image[]tags使用Listingimagetags方法。我试过了funcGetImages(whttp.ResponseWriter,r*http.Request){w.Header().Set("Access-Control-Allow-Origin","*")res,err:=http.Get(fmt.Sprintf("%s/%s",sconf.RegistryConf.url,sconf.Reg

go - 如何将结构的字段传递给函数?

我想知道findById和findByValue是否可以合并为一个函数?那就是除了已有的params之外,还要传struct中的field?import("fmt""errors")typeAstruct{idintvalueint}funcfindById(as[]A,iint)(*A,error){for_,a:=rangeas{ifa.id==i{return&account,nil}}returnnil,errors.New("nosuchitem")}funcfindByValue(as[]A,iint)(A,error){for_,a:=rangeas{ifa.value=

string - golang - 字符串排列 - slice 边界超出范围

Here是这个问题的GoPlayground代码。我正在尝试使用递归编写golang字符串排列。置换函数有两个参数,prefix是一个空字符串("")和str,它是"abc"。代码如下funcmain(){str:="abc"perm("",str)}funcperm(prefixstring,strstring){n:=len(str)fmt.Println(n)ifn==0{fmt.Println(prefix)}else{fori:=0;i当我运行这段代码时,如预期的那样,n的值显示为3,2,1,0。我成功获得了“abc”,但随后我收到了“panic:runtimeerror:s

pointers - 跨包将结构传递给函数

为了更加熟悉go,我正在尝试重构一些已经运行良好的代码。原始代码具有三个结构:typeConfigGroupstruct{IntervalintTprefixstringTarget[]string}typeConfigDefaultsstruct{IntervalintSprefixstring}typeConfigstruct{Groupmap[string]*ConfigGroupDefaultsConfigDefaults}这些结构像这样传递给函数:funcrunpinger(clientStatsdstatsd.Statter,defaults*ConfigDefaults,

go - 如何通过引用传递以便我可以在调用函数中修改它?

我怎样才能将某些东西传递给一个函数,使其可以修改并且可以在调用堆栈中看到?(换句话说如何传递指针或引用?)packagemainimport("os/exec""fmt")funcprocess(names*[]string){fmt.Print("Preprocess",names)names[1]="modified"}funcmain(){names:=[]string{"leto","paul","teg"}process(&names)fmt.Print("Postprocess",names)}Error:invalidoperation:names[0](type*[]s

function - 将函数体/定义作为参数传递给 golang 中的函数调用

最近在使用golang的过程中遇到了如下问题。将函数体传递给函数调用是否可以,例如javascript。例如setTimeout(function(i){console.log("input:",i)},1000).在javascript中将匿名函数传递给另一个函数是很常见的。我想知道在go中是否相同?packagemainimport("fmt")typeHandlerFuncfunc(int)funcmain(){//defineafunctionasobject/variable?hnd:=func(inint){fmt.Println("funchandlerreturnsin

go - 通过引用 holder 对象传递值

以下代码将Holder指定为interface类型。可以对Holder对象进行哪些更改,以便它接收具有引用类型的任何类型,因此如果对值对象进行任何更改,它会反射(reflect)在main上。typeHolderstruct{Bodyinterface{}}typeValuestruct{InputintResultint}funcmain(){value:=Value{Input:5}holder:=Holder{Body:value}fmt.Println(value)//{50}modify(holder)fmt.Println(value)//{50}shoulddisplay

function - 在 Go 中将函数作为参数传递

我尝试在Go中创建一个函数,用于重试任何失败的查询函数(通常是因为序列化问题)。funcretryer(functionAfunc(interface{})(interface{},[]error),maxRetryint,waitBetweentime.Duration)interface{}{//whennoerrorinfunctionA,retryerreturnswhateverfunctionAreturns//whenmaxRetryisreached,returnsnil}我想重试的函数看起来像这样funcGetTopStudent(classIdstring)([]S

go - 找不到包 "api/handlers"

好的,这个问题之前已经有人问过,我相信我已经查看了所有这些问题并测试了答案,但我将解释每个问题为何与我的情况不符。我可能错过了其中一个的答案,但我阅读了每一个并试图看看它是否适合我的情况Howtoimportlocalpackagesingo?我相信我的导入遵循答案的结构Gobuild:"Cannotfindpackage"(eventhoughGOPATHisset)我不确定这个是否完全相关,但我认为这不是同一个错误。Golang-Whycan'tIimportlocalpackageinGOPATH/src/projectbutcaninhomedirectory?我的导入路径不是

unit-testing - 在 golang 中模拟远程 api 调用

我正在尝试更好地编写调用远程api的模拟golang测试我可以使用httptest库很容易地模拟单个调用,但我是在处理多次调用单个端点调用的其他函数时卡住了。例如给定一个简单的创建函数funccreateItem(urlstring,productProduct)(int,error){//makerequestreturncreatedId,nil}我可以写一些像这样的测试funcTestCreateItem(t*testing.T){mock_ts:=httptest.NewServer(http.HandlerFunc(func(whttp.ResponseWriter,r*ht